/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xadrezboladoprototipografico;

import Xadrez.Modelo.Board;

import Xadrez.Modelo.Match;
import Xadrez.Modelo.Move;
import Xadrez.Modelo.Piece;
import Xadrez.Modelo.Square;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.HashMap;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author daniel
 */
public class TabuleiroGrafico {


    void DesenhaTabuleiro(Graphics g, int width, int height, Match board) {
        SetWindowSize(width, height);
        DesenhaTabuleiro(g, board);
    }

    public enum PlayState {

        CHOOSING_PIECE_CPU, CHOOSING_PIECE_PLAYER, CHOOSING_DESTINATION_CPU, CHOOSING_DESTINATION_PLAYER, MOVING_PIECE_CPU, MOVING_PIECE_PLAYER, MATE_CPU, MATE_PLAYER
    };

    private HashMap mapPieceToImage;

    private int CursorX;
    private int CursorY;
    private PlayState State;
    private int CurrentWindowHeight;
    private int CurrentWindowWidth;
    Square square1;
    Square square2;
    Move move;



    TabuleiroGrafico(ResourceMap rm) {
        square1 = new Square();
        square2 = new Square();

        mapPieceToImage = new HashMap();
        CursorX = 0;
        CursorY = 0;
        mapPieceToImage.put("bispoBlack", rm.getImageIcon("Tabuleiro.Pecas[0]").getImage());
        mapPieceToImage.put("bispoWhite", rm.getImageIcon("Tabuleiro.Pecas[1]").getImage());
        mapPieceToImage.put("cavaloBlack", rm.getImageIcon("Tabuleiro.Pecas[2]").getImage());
        mapPieceToImage.put("cavaloWhite", rm.getImageIcon("Tabuleiro.Pecas[3]").getImage());
        mapPieceToImage.put("peãoBlack", rm.getImageIcon("Tabuleiro.Pecas[4]").getImage());
        mapPieceToImage.put("peãoWhite", rm.getImageIcon("Tabuleiro.Pecas[5]").getImage());
        mapPieceToImage.put("reiBlack", rm.getImageIcon("Tabuleiro.Pecas[6]").getImage());
        mapPieceToImage.put("reiWhite", rm.getImageIcon("Tabuleiro.Pecas[7]").getImage());
        mapPieceToImage.put("rainhaBlack", rm.getImageIcon("Tabuleiro.Pecas[8]").getImage());
        mapPieceToImage.put("rainhaWhite", rm.getImageIcon("Tabuleiro.Pecas[9]").getImage());
        mapPieceToImage.put("torreBlack", rm.getImageIcon("Tabuleiro.Pecas[10]").getImage());
        mapPieceToImage.put("torreWhite", rm.getImageIcon("Tabuleiro.Pecas[11]").getImage());
        State = PlayState.CHOOSING_PIECE_PLAYER;

    }

    public void SetWindowSize(int x, int y) {
        CurrentWindowWidth = x;
        CurrentWindowHeight = y;
    }

    public void Click(int x, int y, Match board) {
    int _x=(int) ((x) * 8 / 100);
    int _y=(int) ((y) * 8 / 100);

        switch (State) {
            case CHOOSING_PIECE_PLAYER: {

                if (board.getPiece(_x, _y) != null && board.getPiece(_x, _y).getColor().equals(board.getTurn())) {
                CursorX = _x;
                CursorY = _y;
                    State = PlayState.CHOOSING_DESTINATION_PLAYER;
                    System.out.println("choosing destination");
                }
                return;
            }
            case CHOOSING_DESTINATION_PLAYER:
                System.out.println();
                System.out.println("cursor @" + String.valueOf(CursorX) + "," + String.valueOf(CursorY));
                System.out.println("move to @" + String.valueOf(_x + "," + String.valueOf(_y)));
                System.out.println("is valid?" + String.valueOf(board.isValidMove(CursorX, CursorX, _x, _y)));
                System.out.println(board.getPiece(CursorX, CursorY).getColor());
                System.out.println(board.getTurn());
                if (board.getPiece(CursorX, CursorY) != null) {

                    ///caso o jogador desista e clique em outra peça
                    if (board.getPiece(_x, _y) != null && board.getPiece(_x, _y).getColor().equalsIgnoreCase(board.getPiece(CursorX, CursorY).getColor())) {
                        CursorX = _x;
                        CursorY = _y;
                        System.out.println("still choosing destination");
                        return;
                    }

                  //  if (board.isValidMove(CursorX, CursorX, _x, _y))
                    {
                        board.move(CursorX, CursorY, _x, _y);
                        State = PlayState.CHOOSING_PIECE_CPU;
                        System.out.println("moving piece");
                    }
                }
                break;

            case CHOOSING_PIECE_CPU: {
                CursorX = _x;
                CursorY = _y;
                if (board.getPiece(CursorX, CursorY) != null && board.getPiece(CursorX, CursorY).getColor().equals(board.getTurn())) {
                    State = PlayState.CHOOSING_DESTINATION_CPU;
                    System.out.println("choosing destination");
                }
                return;
            }
            case CHOOSING_DESTINATION_CPU:
                System.out.println();
                System.out.println("cursor @" + String.valueOf(CursorX) + "," + String.valueOf(CursorY));
                System.out.println("move to @" + String.valueOf(_x + "," + String.valueOf(_y)));
                System.out.println("is valid?" + String.valueOf(board.isValidMove(CursorX, CursorX, _x, _y)));
                System.out.println(board.getPiece(CursorX, CursorY).getColor());
                System.out.println(board.getTurn());

                if (board.getPiece(CursorX, CursorY) != null)
                {
                    ///caso o jogador desista e clique em outra peça
                    if (board.getPiece(_x, _y) != null && board.getPiece(_x, _y).getColor().equalsIgnoreCase(board.getPiece(CursorX, CursorY).getColor())) {
                        CursorX = _x;
                        CursorY = _y;
                        System.out.println("still choosing destination");
                        return;
                    }

             //       if (board.isValidMove(CursorX, CursorX, _x, _y))
                    {
                        board.move(CursorX, CursorY, _x, _y);
                        State = PlayState.CHOOSING_PIECE_PLAYER;
                        System.out.println("moving piece");
                    }
                }
                break;

            default:
                break;

        }

     //   System.out.println(String.valueOf(State) + ":" + String.valueOf(CursorX) + "," + String.valueOf(CursorY));
    }



    public void DesenhaTabuleiro(Graphics g, Match board) {
        float ratiox = CurrentWindowWidth / 8;
        float ratioy = CurrentWindowHeight / 8;
        Color cor = Color.RED;
        Image img;
        g.setColor(Color.black);
        Piece piece;

      for (int y = 0; y < 8; y++)  {
           for (int x = 0; x < 8; x++)
             {


                if (((x + y) % 2) == 1) {
                    cor = (Color.BLACK);
                } else {
                    cor = (Color.WHITE);
                }



                if (CursorX == x && CursorY == y) {
                    cor = Color.BLUE;

                }else

                if (State == PlayState.CHOOSING_DESTINATION_PLAYER || State == PlayState.CHOOSING_DESTINATION_CPU)
                {
                System.out.print("is valid:("+String.valueOf(CursorX) +"," +String.valueOf(CursorY) +"),("+String.valueOf(x)+","+String.valueOf(y)+")");
                    if (board.isValidMove(CursorX, CursorX, x, y)) {
                        cor = Color.GREEN;
                      System.out.print(" = valido ");
                    }
                System.out.println();
                }
                
                g.setColor(cor);
                g.fillRect((int) (x * ratiox), (int) (y * ratioy), (int) (ratiox), (int) (ratioy));
                piece = board.getPiece(x, y);
                if (piece != null) {
                    img = (Image) mapPieceToImage.get(piece.toString() + piece.getColor());
                    g.drawImage(img, (int) (x * ratiox), (int) (y * ratioy), (int) ((x + 1) * ratiox), (int) ((y + 1) * ratioy), 0, 0, img.getWidth(null), img.getHeight(null), cor, null);
                }

                

            }
        }



    }
}
